package jsyf.oa.dao.apply;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jsyf.oa.dao.system.LogsDao;
import jsyf.oa.entity.apply.MoneyPayeeEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * @author GUOQI
 */
@Repository
public class MoneyPayeeDao extends BaseDao {

    @Autowired
    LogsDao logsDao;

    public MoneyPayeeDao() {
        super.iBaseEntity = new MoneyPayeeEntity();
    }

    public List<MoneyPayeeEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<MoneyPayeeEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            MoneyPayeeEntity moneyPayeeEntity = (MoneyPayeeEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(moneyPayeeEntity);
        }
        return entityList;
    }

    public List<MoneyPayeeEntity> getEntityById(String condition, Object[] params) {
        return null;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, MoneyPayeeEntity.class);
    }

    @Override
    public Integer removeByCondition(String condition, String[] params) {
        String mainId = params[0];
        if (null != params && params.length > 0 && null != mainId) {
            StringBuffer sql = new StringBuffer();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = super.getCurrentTime();
            String dateFormat = format.format(date);
            sql.append(" UPDATE " + iBaseEntity.getTableName() + " SET LOG_FLAG=1 \n" +
                    " , UPDATE_TIME = str_to_date('" + dateFormat + "', '%Y-%m-%d %H:%i:%s')\n" +
                    " WHERE 1 = 1 \n" +
                    " AND LOG_FLAG=0 \n");
            sql.append(condition);
            this.getJdbcTemplate().update(sql.toString(), params);
            return 1;
        } else {
            return 0;
        }
    }

    public List<JSONObject> getPayeeList(String userId, String payeeName) {
        if (!StringUtils.isEmpty(payeeName)) {
            payeeName = payeeName.replace("'", "");
        }
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "\tID AS id ,\n" +
                "\tPAYEE_NAME AS payeeName ,\n" +
                "\tBANK_NAME AS bankName ,\n" +
                "\tBANK_CARD_NUMBER AS bankCardNumber ,\n" +
                "\tUPDATE_USER_ID AS userId ,\n" +
                "\tUPDATE_TIME AS updateTime\n" +
                "FROM\n" +
                "\tt_apl_money_payee\n" +
                "WHERE\n" +
                "\t1 = 1\n" +
                "AND PAYEE_NAME LIKE '%" + payeeName + "%'\n" +
                "AND BANK_NAME is not null\n" +
                "AND UPDATE_USER_ID = '" + userId + " '\n" +
                "GROUP BY\n" +
                "\tBANK_CARD_NUMBER\n" +
                "ORDER BY\n" +
                "\tUPDATE_TIME DESC\n" +
                "LIMIT 10");
        return super.queryForJsonList(sql.toString());
    }
}
